package cn.bisoft.java.service.ext.httpclient.core.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import cn.bisoft.java.service.ext.httpclient.core.BaseHttpService;
import cn.bisoft.java.service.ext.httpclient.core.HttpService;

public class HttpServiceImpl extends BaseHttpService implements HttpService
{
	private static final Log DEBUG_LOGGER = LogFactory.getLog(HttpServiceImpl.class);

	private ThreadLocal<String> resultHolder = new ThreadLocal<String>();

	@Override
	protected void execute()
	{
		DEBUG_LOGGER.info("[REQUEST]\n\r[" + getMethod() + "] " + getUri());

		try
		{
			HttpResponse response = client.execute(httpMethod);

			HttpEntity entity = response.getEntity();

			byte[] result = EntityUtils.toByteArray(entity);

			String rtnMsg = new String(result).trim();

			DEBUG_LOGGER.info("[RESPONSE]\n\r" + rtnMsg);
			
			resultHolder.set(rtnMsg);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	@Override
	public String getResponse()
	{
		return resultHolder.get();
	}
}
